/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TUILayer;

import DomainLayer.*;
import utils.*;
import java.util.Scanner;

/**
 *
 * @author Middreks
 * Licensed under GNU/GPLv2
 * Credits go to Group 4
 */
public class DVDUI
{
    private Tracking tracking;
    private IdGenerator dvdId;
    private IdGenerator copySN;
    private int selection;
    private Scanner input;
    private String string;

    public DVDUI(Tracking tracking)
    {
        input = new Scanner(System.in);
        dvdId = new IdGenerator();
        copySN = new IdGenerator();
        selection = -1;
        string = "";
        this.tracking = tracking;
    }

    /**
     * Menu of the DVD Management
     */
    public void dvdMenu()
    {

        while (selection != 0)
        {
            System.out.printf("Please choose from the following\n"
                    + "\t*1* List DVDs\n"
                    + "\t*2* Add DVD\n"
                    + "\t*3* Edit DVD\n"
                    + "\t*4* Remove DVD\n"
                    + "\n"
                    + "\t*0* Exit to Main Menu\n");

            if (input.hasNextInt())
            {
                selection = input.nextInt();
                switch (selection)
                {
                    case 0:
                        break;
                    case 1:
                        ConsoleUtils.clear();
                        if (tracking.getDvds().length == 0)
                            System.out.println("No DVDs have been added.");
                        else
                            Printer.print(tracking.getDvds());
                        break;
                    case 2:
                        ConsoleUtils.clear();
                        addDVD();
                        break;
                    case 3:
                        ConsoleUtils.clear();
                        if (tracking.getDvds().length == 0)
                            System.out.println("No DVDs have been added.");
                        else
                            editDVD();
                        break;
                    case 4:
                        ConsoleUtils.clear();
                        if (tracking.getDvds().length == 0)
                            System.out.println("No DVDs have been added.");
                        else
                            removeDVD();
                        break;
                    default:
                        System.out.println(" Incorrect input!");
                }
            } else
            {
                string = input.next();
                ConsoleUtils.clear();
                System.out.printf("%s is not a number!\n", string);
            }

        }
        selection = -1;
        ConsoleUtils.clear();
    }

    private void addDVD()
    {
        String title, artist, publicationDate;
        title = artist = publicationDate = "";
        int nrOfCopies, price;
        nrOfCopies = price = -1;

        System.out.print("Enter title: ");
        if (input.hasNext())
            title = input.next();
        System.out.print("Enter artist: ");
        if (input.hasNext())
            artist = input.next();
        System.out.print("Enter publication date: ");
        if (input.hasNext())
            publicationDate = input.next();
        do
        {
            System.out.print("How many copies should be added: ");
            if (input.hasNextInt())
                nrOfCopies = input.nextInt();
            else
            {
                System.out.println("Error: number must be entered!");
                input.next();
            }
        } while (nrOfCopies == -1);

        do
        {
            System.out.print("What's the price: ");
            if (input.hasNextInt())
                price = input.nextInt();
            else
            {
                System.out.println("Error: number must be entered!");
                input.next();
            }
        } while (price == -1);
        int id = dvdId.getNextId();
        int SN;

        DVD dvd = new DVD(id, title, artist, publicationDate);
        for (int i = 0; i < nrOfCopies; i++)
        {
            SN = copySN.getNextId();
            tracking.addCopy(new Copy(SN, "no", price, dvd));
        }

        ConsoleUtils.clear();
        System.out.println("DVD was successfully added.");
    }

    private void editDVD()
    {
        DVD[] dvds = tracking.getDvds();
        while (selection != 0)
        {
            Printer.print(dvds);
            System.out.print("Enter the number of the DVD you want to edit" +
                    " or 0 to go back: ");
            if (input.hasNextInt())
            {
                selection = input.nextInt();
                if (selection <= dvds.length && selection > 0)
                {
                    int temp = selection;
                    ConsoleUtils.clear();
                    while (selection != 0)
                    {
                        System.out.print("Choose what information you would " +
                                "like to edit or just hit 0 to go back \n" +
                                "*1* Title\n" +
                                "*2* Artist\n" +
                                "*3* Publication Date\n");
                        if (input.hasNextInt())
                        {
                            selection = input.nextInt();

                            switch (selection)
                            {
                                case 0:
                                    break;
                                case 1:
                                    ConsoleUtils.clear();
                                    System.out.print("Enter new title: ");
                                    if (input.hasNext())
                                    {
                                        string = input.next();
                                        dvds[temp - 1].setTitle(string);
                                    }
                                    break;
                                case 2:
                                    ConsoleUtils.clear();
                                    System.out.print("Enter new artist: ");
                                    if (input.hasNext())
                                    {
                                        string = input.next();
                                        dvds[temp-1].setArtist(string);
                                    }
                                    break;
                                case 3:
                                    ConsoleUtils.clear();
                                    System.out.print("Enter new publication " +
                                            "date: ");
                                    if (input.hasNext())
                                    {
                                        string = input.next();
                                        dvds[temp-1].setPublicationDate(string);
                                    }
                                    break;
                                default:
                                    ConsoleUtils.clear();
                                    System.out.println("Incorrect input!");
                            }
                        }
                        else
                        {
                            ConsoleUtils.clear();
                            string = input.next();
                            System.out.printf("%s is not a number!\n",
                                    string);
                        }
                    }
                }
                else if (selection != 0)
                {
                    ConsoleUtils.clear();
                    System.out.println("Incorrect input!\n");
                }
            }
            else
            {
                string = input.next();
                ConsoleUtils.clear();
                System.out.printf("%s is not a number!\n", string);
            }
        }

        selection = -1;
        ConsoleUtils.clear();
    }

    private void removeDVD()
    {
        DVD[] dvds = tracking.getAvailableDVDs();
        while (selection != 0)
        {
            if (dvds.length == 0)
                System.out.println("There are no DVDs.");
            else
                Printer.print(dvds);
            
            System.out.print("Enter the number of the DVD you want to remove" +
                    " or 0 to go back: ");
            if (input.hasNextInt())
            {
                selection = input.nextInt();
                if (selection <= dvds.length && selection > 0)
                {
                    dvdId.removeSavedId(dvds[selection - 1].getId());
                    tracking.removeCopiesOfDVD(dvds[selection - 1]);
                    copySN.setSavedIds(tracking.getSerialsOfCopies());
                    dvds = tracking.getAvailableDVDs();
                }
                else if (selection != 0)
                {
                    ConsoleUtils.clear();
                    System.out.println(" Incorrect input!\n");
                }
            }
            else
            {
                    string = input.next();
                    ConsoleUtils.clear();
                    System.out.printf("%s is not a number!\n", string);
            }
        }

        selection = -1;
        ConsoleUtils.clear();
    }
}
